package com.example.alipay.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.alipay.domain.AlipayBean;
import com.example.alipay.domain.PayRecord;
import com.example.alipay.service.PayRecordService;
import com.example.alipay.service.PayService;
import com.example.common.Result;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/4/10 3:30
 * @since JDK 1.8
 */
@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private PayService payService;

    /**
     * 阿里支付
     * @param subject
     * @param body
     * @return
     * @throws AlipayApiException
     */
    @GetMapping(value = "alipay")
    @ApiModelProperty("创建支付信息")
    public String alipay(String outTradeNo, String subject, String totalAmount, String body) throws AlipayApiException {
        AlipayBean alipayBean = new AlipayBean();
        alipayBean.setOut_trade_no(outTradeNo);
        alipayBean.setSubject(subject);
        alipayBean.setTotal_amount(totalAmount);
        alipayBean.setBody(body);
        return payService.aliPay(alipayBean);
    }

    @PostMapping("createOrd")
    @ApiOperation("创建二维码订单")
    public JSONObject createCode111Controller(@ApiParam("商品名称")@RequestParam("subject")String subject,
                                              @ApiParam("商品金额")@RequestParam("money")String totalAmount,
                                              @ApiParam("商品描述")@RequestParam("body")String body
    , HttpServletResponse response)throws AlipayApiException {
        AlipayBean alipayBean = new AlipayBean();
        alipayBean.setSubject(subject);
        alipayBean.setTotal_amount(totalAmount);
        alipayBean.setBody(body);
        String content=payService.aliPay(alipayBean);
        String url="/img/createercode?content="+content;
        return Result.successInfo("url",url);
    }

    @Autowired
    private PayRecordService payRecordService;


    @PostMapping("list")
    @ApiModelProperty("创建订单列表")
    public JSONObject getRecordListController(@ApiParam("页码")@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                              @ApiParam("条数")@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                              @ApiParam("关键字")@RequestParam(value = "keyword",required = false)String keyword){
        LambdaQueryWrapper<PayRecord> queryWrapper= Wrappers.lambdaQuery();
        if(StringUtils.hasText(keyword)){
            queryWrapper.like(PayRecord::getSubject,keyword);
            queryWrapper.or().like(PayRecord::getBody,keyword);
            queryWrapper.or().like(PayRecord::getTotalMount,keyword);
        }
        IPage<PayRecord> payRecordIPage=payRecordService.page(new Page(pageNum,pageSize),queryWrapper);
        return Result.successObject(payRecordIPage.getTotal(),payRecordIPage.getRecords());
    }



}
